const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

// // 查询历史订单
// router.get('/gethistoryorder',function(req,res){
//     let mysql=`SELECT order_id,order_num,customer_name,order_time,customer_type,cash_money,pay_way,table_num,order_state FROM order_information 
//                JOIN customer ON order_information.order_username=customer.customer_id
//                JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
//                JOIN cash_flowing ON order_information.order_id=cash_flowing.cash_num
//                JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id GROUP BY order_num`
//     db(mysql,null,function(err,data){
//         if(err){
//             return res.json({
//                 code: 500,
//                 msg: '查询历史订单失败，请稍后重试'
//             })
//         }
//         res.json({
//             code: 200,
//             msg: '查询历史订单成功',
//             data: data
//         })
//     })
// })

// 模糊查询历史订单
router.get('/gethistoryorder',function(req,res){
    let mysql=`SELECT order_id,order_num,customer_name,order_time,customer_type,cash_money,pay_way,table_num,order_state FROM order_information 
               JOIN customer ON order_information.order_username=customer.customer_id
               JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
               JOIN cash_flowing ON order_information.order_id=cash_flowing.cash_num
               JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id
               WHERE 1=1 `
    if(req.query.orderId){
        mysql+=`AND order_num LIKE '%${req.query.orderId}%'`
    }
    if(req.query.payWay){
        mysql+=`AND pay_way='${req.query.payWay}'`
    }
    mysql+=` GROUP BY order_num`
    db(mysql,null,function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '查询历史订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询历史订单成功',
            data: data
        })
    })
})

// 删除历史订单
router.post('/delhistoryorder',function(req,res){  
    let mysql='DELETE FROM order_information WHERE order_id=?';
    db(mysql,[req.body.order_id],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '删除订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除订单成功',
            data: data
        })
    })
})

// 修改历史订单状态
router.post('/updatehistoryorderstate',function(req,res){  
    let mysql=`UPDATE order_information SET order_state='未付款' WHERE order_id=?`;
    db(mysql,[req.body.order_id],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '修改订单状态失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '修改订单状态成功',
            data: data
        })
    })
})

module.exports = router